<?php 
/*
* ----------------------------------------------------------------------------------------------------
* CONTACT, COMMENT WIDTH RECAPTCHAR FUNCTIONS
* @PACKAGE BY TT
* ----------------------------------------------------------------------------------------------------
*/



//GET RECAPTCHAR
function the_recaptchar($in_contact_or_blog)
{
	if(get_theme_option($in_contact_or_blog,$in_contact_or_blog.'_enable_recaptchar') == 'true' && is_user_must_using_captchar() == true)
        {
           $publickey=get_theme_option('general','recaptchar_Public_Key');
		   if($publickey =='')
		   {
		   echo esc_html('Recaptchar Public key Not Found','TT');
		   }else
		   {
           echo recaptcha_get_html($publickey);
		   }
        }
}
//GET RECAPTCHAR
function the_recaptchar_with_filter($in_contact_or_blog)
{
	if(get_theme_option($in_contact_or_blog,$in_contact_or_blog.'_enable_recaptchar') == 'true' && is_user_must_using_captchar() == true)
        {
           $publickey=get_theme_option('general','recaptchar_Public_Key');
		   if($publickey =='')
		   {
		   echo esc_html('Recaptchar Public key Not Found','TT');
		   }else if(is_user_must_using_captchar() == true)
		   {
           echo recaptcha_get_html($publickey);
		   }
        }
}
//GET RECAPTCHAR
function get_the_recaptchar($in_contact_or_blog)
{
	if(get_theme_option($in_contact_or_blog,$in_contact_or_blog.'_enable_recaptchar') == 'true' && is_user_must_using_captchar() == true)
        {
           $publickey=get_theme_option('general','recaptchar_Public_Key');
		   if($publickey =='')
		   {
		   return esc_html('Recaptchar Public key Not Found','TT');
		   }else
		   {
           return recaptcha_get_html($publickey);
		   }
        }
}
//GET RECAPTCHAR
function get_the_recaptchar_with_filter($in_contact_or_blog)
{
	if(get_theme_option($in_contact_or_blog,$in_contact_or_blog.'_enable_recaptchar') == 'true' && is_user_must_using_captchar() == true)
        {
           $publickey=get_theme_option('general','recaptchar_Public_Key');
		   if($publickey =='')
		   {
		   return esc_html('Recaptchar Public key Not Found','TT');
		   }else if(is_user_must_using_captchar() == true)
		   {
           return recaptcha_get_html($publickey);
		   }
        }
}


// Add filter whene comment submit :
function is_user_must_using_captchar()
{
 	$recaptchar_hide_when = get_theme_option('general','recaptchar_hide_when');
	if(current_user_can($recaptchar_hide_when))
	{
       return false;
	}
	else
	{
	   return true; 
	}
}

function filter_handler($id)
{
 		 global $error_recaptchar;
		 $error_recaptchar = '';
		 $blog_enable_recaptchar= get_theme_option('blog','blog_enable_recaptchar');
 		 if($blog_enable_recaptchar == true && is_user_must_using_captchar() == true):
               $privatekey = get_theme_option('general','recaptchar_Private_Key');
           	if($privatekey <> ''):
                 $google_recaptchar_verify = google_recaptchar_verify($privatekey,$_POST['recaptcha_challenge_field'],$_POST['recaptcha_response_field']);
                   if($google_recaptchar_verify <> 'success' ):
                     $result = esc_html__('Captchar have a mistake!','TT');
					 $_SESSION['recaptchar_comment_error'] = 'Captchar have a mistake';
					 wp_set_comment_status($id, 'trash');
				   endif;
           	else:
           		  $result= esc_html__('Recaprtchar private key not found','TT');
				  $_SESSION['recaptchar_comment_error'] = 'Recaprtchar private key not found';
				  wp_set_comment_status($id, 'trash');
           	endif;	
		endif;
		
         
}
add_action('comment_post', 'filter_handler');


function ajaxcomment_callback()
{
    $result = 'true';
	$blog_enable_recaptchar= get_theme_option('blog','blog_enable_recaptchar');
    if($blog_enable_recaptchar == true && is_user_must_using_captchar() == true):
        $privatekey = get_theme_option('general','recaptchar_Private_Key');
    	if($privatekey <> ''):
          $google_recaptchar_verify = google_recaptchar_verify($privatekey,$_POST['recaptcha_challenge_field'],$_POST['recaptcha_response_field']);
            if($google_recaptchar_verify <> 'success' ):
              $result = esc_html__('Captchar have a mistake!','TT');
			  $_SESSION['captchar_comment_error'] = 'Captchar have a mistake!';
            else :
          	  $result = 'true';
       	    endif;
    	else:
    		
    	endif;	
	else:
	
	endif;
	echo $result;
	die();
}
add_action('wp_ajax_nopriv_ajaxcomment', 'ajaxcomment_callback');
add_action('wp_ajax_ajaxcomment', 'ajaxcomment_callback');

// Ajax Contact
function ajaxcontact_callback()
{
		$contact_email = get_theme_option('contact','email');
    	//Enter your email address, email from contact form will send to this addresss. Please enter inside quotes ('myemail@email.com')
    	define('DEST_EMAIL', $contact_email);
    	//Change email subject to something more meaningful
    	define('SUBJECT_EMAIL',$_POST['subject']);
    	//Error message when message can't send
    	define('SUCCESS_MESSAGE', '<h4 class="form_thanks">Thanks! Our Team will get in touch in next 24 hours.<h4>');
    	//Thans message when message  sended
    	define('ERROR_MESSAGE', '<h4 class="form_thanks">Oops! something went wrong, please try to submit later.</h4>');
		$result='';
    	$from_name = $_POST['name'];
    	$from_email = $_POST['email'];
    	$mime_boundary_1 = md5(time());
        $mime_boundary_2 = "1_".$mime_boundary_1;
        $mail_sent = false;
        # Common Headers
        $headers = "";
        $headers .= 'From: '.$from_name.'<'.$from_email.'>'.PHP_EOL;
        $headers .= 'Reply-To: '.$from_name.'<'.$from_email.'>'.PHP_EOL;
        $headers .= 'Return-Path: '.$from_name.'<'.$from_email.'>'.PHP_EOL;        // these two to set reply address
        $headers .= "Message-ID: <".date('Y-m-d H:i:s')."webmaster@".$_SERVER['SERVER_NAME'].">";
        $headers .= "X-Mailer: PHP v".phpversion().PHP_EOL;                  // These two to help avoid spam-filters
        # Boundry for marking the split & Multitype Headers
        $headers .= 'MIME-Version: 1.0'.PHP_EOL;
        $headers .= "Content-Type: multipart/mixed;".PHP_EOL;
        $headers .= "   boundary=\"".$mime_boundary_1."\"".PHP_EOL;
    	$message = 'Name: '.$from_name.PHP_EOL;
    	$message.= 'Email: '.$from_email.PHP_EOL.PHP_EOL;
    	$message.= 'Message: '.PHP_EOL.$_POST['comment'].PHP_EOL.PHP_EOL;
    	if(isset($_POST['website']))
    	{
    		$message.= 'Website: '.$_POST['website'].PHP_EOL;
    	}
		if(isset($_POST['phone']))
    	{
    		$message.= 'Phone: '.$_POST['phone'].PHP_EOL;
    	}
		if(isset($_POST['address']))
    	{
    		$message.= 'Address: '.$_POST['address'].PHP_EOL;
    	}
    $result="success";
	$contact_enable_recaptchar= get_theme_option('contact','contact_enable_recaptchar');
    if($contact_enable_recaptchar == true && is_user_must_using_captchar() == true):
        $privatekey = get_theme_option('general','recaptchar_Private_Key');
    	if($privatekey <> ''):
          $google_recaptchar_verify = google_recaptchar_verify($privatekey,$_POST['recaptcha_challenge_field'],$_POST['recaptcha_response_field']);
            if($google_recaptchar_verify <> 'success' ):
              $result = '<h4 class="form_thanks">Captchar have a mistake!</div>';
            else :
				 if(mail(DEST_EMAIL, SUBJECT_EMAIL, $message, $headers))
				 {
    			 $result = SUCCESS_MESSAGE;
				 } else
				 {
				 $result = ERROR_MESSAGE;
				 } 
       	    endif;
    	else:
    		  $result= '<h4 class="form_thanks">Recaprtchar private key not found</div>';
    	endif;		     
	elseif(($contact_enable_recaptchar == false) || ($contact_enable_recaptchar == true && is_user_must_using_captchar() == false)):
				 if(mail(DEST_EMAIL, SUBJECT_EMAIL, $message, $headers))
				 {
    			 $result = SUCCESS_MESSAGE;
				 } else
				 {
				 echo DEST_EMAIL.PHP_EOL;
				 echo SUBJECT_EMAIL.PHP_EOL;
				 echo $message;
				 echo  $headers;
				 $result = ERROR_MESSAGE;
				 };   
	else:
		  $result = ERROR_MESSAGE;  
	endif;
	echo $result;
	die();
}
add_action('wp_ajax_nopriv_ajaxcontact', 'ajaxcontact_callback');
add_action('wp_ajax_ajaxcontact', 'ajaxcontact_callback');

function ajax_wgcontact_callback()
{
    //we need to get our variables first

    $email_to =   get_theme_option('contact','email'); //the address to which the email will be sent
    $name     =   $_POST['name'];  
    $email    =   $_POST['email'];
    if(isset($_POST['subject'])){
	$subject  =   $_POST['subject'];
    }else
	{
	$subject = "quick contact";
	}
	$message  =   $_POST['message'];
	
    /*the $header variable is for the additional headers in the mail function,
     we are asigning 2 values, first one is FROM and the second one is REPLY-TO.
     That way when we want to reply the email gmail(or yahoo or hotmail...) will know 
     who are we replying to. */
    $headers  = "From: $email\r\n";
    $headers .= "Reply-To: $email\r\n";
    
    if(mail($email_to, $subject, $message, $headers)){
        echo esc_html__('Thank for contact with us!','TT'); // we are sending this text to the ajax request telling it that the mail is sent..      
    }else{
	    
        echo esc_html__('Error!');// ... or this one to tell it that it wasn't sent    
    }
	die();
}
add_action('wp_ajax_nopriv_wgcontact', 'ajax_wgcontact_callback');
add_action('wp_ajax_wgcontact', 'ajax_wgcontact_callback');


?>